from commons.db import MongoClient
from commons.log import LogFactory
from commons.singleton import singleton
from models.message_model import MsgInfoModel


@singleton
class BlockedRoomFilter:
    """
    过滤掉不想看到的群聊消息
    """
    logger = LogFactory("blocked_room_filter").get_logger()

    def __init__(self):
        self.logger.info('init BlockedRoomFilter')

    def filter(self, msg) -> bool:
        message = MsgInfoModel(msg)
        room_name = message.room_name
        # 获取被阻止的群聊名
        # TODO:用redis缓存这部分数据，设置过期时限
        self.block_list = MongoClient.get_blocked_room()
        return room_name in self.block_list
